package queryTree;

/**
 * @author Van
 */

import java.util.ArrayList;

import optimization.Condition;




public class JoinNode extends AbstractNode {
	
	public int joinAlgorithm = -1;
	public int joinStrategy = -1;
	public String thirdSite = null;
	public ArrayList<String> tables = new ArrayList<String>();
	public void addJoinCondition(Condition c) {
		joinConditionList.add(c);
	}
	
	
	TreeNode leftJoinTree;
	TreeNode rightJoinTree;
	//list of join conditions. If list is null or empty, it is a cartesian.
	ArrayList<Condition> joinConditionList = new ArrayList<Condition>();
	
	
	public Object clone() throws CloneNotSupportedException
	{
		JoinNode newObject= new JoinNode();
		if(leftJoinTree !=null && rightJoinTree != null)
		{
			newObject.setLeftJoinTree((TreeNode)leftJoinTree.clone());
			newObject.setRightJoinTree((TreeNode)rightJoinTree.clone());
		}
		return newObject;
	}
	public String toString()
	{
		String result="";
		for(int i = 0; i < joinConditionList.size(); i++) {
			Condition con = joinConditionList.get(i);
			String s = con.lhs.item_name.toString() + " " + Common.getOperator(con.operator.ordinal()) +
						con.rhs.item_name.toString();	
			if (i == 0) {
				result = s;			
			} else {
				result += " AND " + s; 
			}
			
		}
		
		result = result + "A:"+joinAlgorithm+" S:"+joinStrategy;
		
		return result;
	}
	public TreeNode getLeftJoinTree() {
		return leftJoinTree;
	}
	public void setLeftJoinTree(TreeNode leftJoinTree) {
		this.leftJoinTree = leftJoinTree;
	}
	public TreeNode getRightJoinTree() {
		return rightJoinTree;
	}
	public void setRightJoinTree(TreeNode rightJoinTree) {
		this.rightJoinTree = rightJoinTree;
	}
	
	public ArrayList<Condition> getJoinConditionList() {
		return joinConditionList;
	}
	public void setJoinConditionList(ArrayList<Condition> joinConditionList) {
		this.joinConditionList = joinConditionList;
	}
	public void getTableList(ArrayList<String> tableList) {
		leftJoinTree.getTableList(tableList);
		rightJoinTree.getTableList(tableList);
	}
	
	public void addJoinConditions(ArrayList<Condition> list) {
		joinConditionList.addAll(list);
	}
}
